<template>
  <div class="User">
    <!-- 头部 -->
    <div class="User_top">
      <!-- 头像 -->
      <img :src="headImg" alt="" />
      <!-- 登录时的用户状态 -->
      <h3 v-if="isLogined">{{ username }}</h3>
      <h3 v-else>点击登录</h3>
      <van-icon :name="!isLogined ? 'arrow' : 'cross'" @click="loginClick" />
    </div>
    <!-- 九宫格 -->
    <van-grid square :column-num="3">
      <van-grid-item
        v-for="item in gridArr"
        :key="item.id"
        :icon="item.icon"
        :text="item.type"
      />
    </van-grid>
    <!-- 模态框 -->
    <!-- 表单登录 -->
    <!-- 模态框 -->
    <div class="modal" v-show="isshowmodal">
      <div class="modal_bg" @click="isshowmodal = false"></div>
      <div class="modal_content">
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
import { LoginFn } from "@/utils/http";
//引入头像
import headUserImg from "../assets/images/touxiang.webp";
import headImg from "../assets/images/touxiang.png";
export default {
  props: {},
  data() {
    return {
      isLogined: false, //定义登录状态
      username: "", //表单内容
      password: "", //表单内容
      isshowmodal: false, //模态框隐藏
      headImg: headUserImg, //头像引入
      gridArr: [
        // grid数组
        { id: 0, icon: "label-o", type: "我的订单" },
        { id: 1, icon: "bill-o", type: "优惠券" },
        { id: 2, icon: "goods-collect-o", type: "礼品卡" },
        { id: 3, icon: "location-o", type: "我的收藏" },
        { id: 4, icon: "flag-o", type: "我的足迹" },
        { id: 5, icon: "contact", type: "会员福利" },
        { id: 6, icon: "aim", type: "地址管理" },
        { id: 7, icon: "warn-o", type: "账号安全" },
        { id: 8, icon: "service-o", type: "联系客服" },
        { id: 9, icon: "question-o", type: "帮助中心" },
        { id: 10, icon: "smile-comment-o", type: "意见反馈" },
      ],
    };
  },
  created() {
    const userinfo = JSON.parse(localStorage.getItem("userinfo"));
    if (userinfo) {
      //存在就已经登录
      this.isLogined = true;
      this.username = userinfo.username;
      this.headImg = headUserImg;
    } else {
      //w未登录
      this.headImg = headImg;
    }
  },
  computed: {
    //头像切换
    headChange() {
      if (userinfo) {
        //存在就已经登录
        return this.headImg = headUserImg;
      } else {
        //w未登录
        return this.headImg = headImg;
      }
    },
  },
  methods: {
    onSubmit(values) {
      // console.log(e);
      LoginFn({ username: values["用户名"], pwd: values["密码"] }).then(
        (res) => {
          console.log(res);
          if (res.errno == 0) {
            //登录成功
            //修改登录状态
            this.isLogined = true;
            //将TOKEN存在本地
            localStorage.setItem("token", res.data.token);
            //将用户信息存入本地
            localStorage.setItem("userinfo", JSON.stringify(res.data.userInfo));
            //将用户信息展示
            this.username = res.data.userInfo.username;
            // this.headImg=res.data.userInfo.avatar
            this.isshowmodal = false;
          } else {
            //登录失败
          }
        }
      );
    },
    //点击登录
    loginClick() {
      if (this.isLogined) {
        //登录成功
        this.$dialog
          .confirm({
            title: "警示",
            message: "真的舍得走么",
          })
          .then(() => {
            //确认按钮
            //清除本地用户数据清除,设置状态未登录
            this.isLogined = false;
            localStorage.removeItem("userinfo");
          })
          .catch((err) => {
            //取消
            console.log(err);
          });
      } else {
        //w未登录
        //模态框显示
        this.isshowmodal = true;
        this.username = "";
        this.password = "";
      }
    },
  },
};
</script>
<style lang='less' scoped>
.User {
  .User_top {
    background: #333;
    color: #fff;
    padding: 20px 10px;
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 16px;
    img {
      border-radius: 50%;
      margin-right: 10px;
      width: 70px;
      height: 70px;
    }
    h3 {
      flex: 1;
    }
  }
  .modal {
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    left: 0;
    top: 0;
    .modal_bg {
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
    }
    .modal_content {
      width: 90%;
      height: 200px;
      position: absolute;
      background: #fff;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      padding-top: 40px;
    }
  }
}
</style>